<?php

/**
 * Description of AdminControllerBase
 *
 * @author HEO
 */
class AdminControllerBase extends SBaseController {

    public $adminTheme = 'admin';
    public $layout = '//layouts/main';
    public $menu = array();
    public $breadcrumbs = array();
    public $title = 'Mobile Store';

    public function init() {
        Yii::app()->theme = $this->adminTheme;
        Yii::app()->format->dateFormat = 'd-m-Y';
//        Yii::app()->name = 'Mobile Store :: Administrator';
        Yii::app()->params['config'] = unserialize(Variable::model()->findByPk('config')->value);
        if (Yii::app()->user->getState('pageSize') < 1)
            Yii::app()->user->setState('pageSize', (int) Yii::app()->params['config']['limitAdminPage']);
    }

    /**
     * Set a flash message display to user.
     * @param string $message 
     */
    public function setFlash($message) {
        Yii::app()->session['flash'] = $message;
    }

    /**
     * Check flash is exist or not.
     * @return boolean
     */
    public function hasFlash() {
        return (isset(Yii::app()->session['flash']));
    }

    /**
     * Retrieve the flash message.
     */
    public function getFlash() {
        if ($this->hasFlash()) {
            $message = Yii::app()->session['flash'];
            unset(Yii::app()->session['flash']);
            return $message;
        }
    }

    public function renderFlash() {
        if ($this->hasFlash()) {
            echo '<div class="flash-notice">';
            echo $this->getFlash();
            echo '</div>';
            Yii::app()->clientScript->registerScript('fade', "setTimeout(function() { $('.flash-notice').fadeOut('slow'); }, 5000);");
        }
    }

    protected function performAjaxValidation($model, $form) {
        if (isset($_POST['AJAX']) && $_POST['AJAX'] == $form) {
            echo CActiveForm::validate($models);
            Yii::app()->end();
        }
    }

}
